﻿using System;
using System.Collections.Generic;
using System.Text;
using WAD.Model;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Data.Common;

namespace WAD.DAL
{
    public class DAOFCOrder : DAO
    {
        public static bool Add(MFCOrder m)
        {
            String sql = "insert into fc_order([user_id],article_id,unit_price,total_price,pay_price,number,orderid,state,agentid,platform,date)values(@user_id,@article_id,@unit_price,@total_price,@pay_price,@number,@orderid,@state,@agentid,@platform,@date)";
            Database db = GetDatabase();
            DbCommand cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd, "@user_id", DbType.Int32, m.user_id);
            db.AddInParameter(cmd, "@article_id", DbType.Int32, m.article_id);
            db.AddInParameter(cmd, "@unit_price", DbType.Decimal, m.unit_price);
            db.AddInParameter(cmd, "@total_price", DbType.Decimal, m.total_price);
            db.AddInParameter(cmd, "@pay_price", DbType.Decimal, m.pay_price);
            db.AddInParameter(cmd, "@number", DbType.Int32, m.number);
            db.AddInParameter(cmd, "@orderid", DbType.String, m.orderid);
            db.AddInParameter(cmd, "@state", DbType.Int32, m.state);
            db.AddInParameter(cmd, "@agentid", DbType.Int32, m.agentid);
            db.AddInParameter(cmd, "@platform", DbType.Int32, m.platform);
            db.AddInParameter(cmd, "@date", DbType.DateTime, m.date.Value);
            return db.ExecuteNonQuery(cmd) > 0;
        }

        public static MFCOrder Get(string orderid)
        {
            if (string.IsNullOrEmpty(orderid)) return null;
            var sql = "select [id],[user_id],[article_id],[unit_price],[total_price],[pay_price],[number],[orderid],[state],[agentid],[platform],[callback_msg],[callback_date],[date]  from fc_order where orderid=@orderid";
            var db = GetDatabase();
            var cmd = db.GetSqlStringCommand(sql);
            db.AddInParameter(cmd,"@orderid",DbType.String,orderid);
            using (var reader = db.ExecuteReader(cmd))
            {
                if (!reader.Read()) return null;
                var result = new MFCOrder
                {
                    id = reader.GetInt32(0),
                    user_id = reader.GetInt32(1),
                    article_id = reader.GetInt32(2),
                    unit_price = reader.GetDecimal(3),
                    total_price = reader.GetDecimal(4),
                    pay_price = reader.GetDecimal(5),
                    number = reader.GetInt32(6),
                    orderid = reader.GetString(7),
                    state = reader.GetInt32(8)
                };
                if (!reader.IsDBNull(9))
                    result.agentid = reader.GetInt32(9);
                
                if (!reader.IsDBNull(10))
                    result.platform = reader.GetInt32(10);
                if (!reader.IsDBNull(11))
                    result.callback_msg = reader.GetString(11);
                if (!reader.IsDBNull(12))
                    result.callback_date = reader.GetDateTime(12);
                result.date = reader.GetDateTime(13);
                return result;
            }
        }

        public static int UpdateCallBack(string orderid, int state, string callbackMsg, string callbackDate)
        {
            if (string.IsNullOrEmpty(orderid)) return 0;
            return ExecuteNonQuery(string.Format("update fc_order set callback_msg='{0}',callback_date='{1}',state={2} where orderid='{3}'"
                                                , callbackMsg, callbackDate, state, orderid));
        }

        public static bool Exists(MFCOrder m)
        {
            return false;
        }
    }
}